const xlsx = require('./../lib/xlsx-to-config.js');
const path = require('path');
const config = require('./../config.json');
const fs = require('fs');

export function exportJson() {
    let pathDestJson = path.join(__dirname, config.debug ? config.debug_xlsx.dest : config.xlsx.dest);
    let pathDestTs = path.join(__dirname, config.debug ? config.debug_xlsx.dest_ts : config.xlsx.dest_ts);
    let pathDestLua = path.join(__dirname, config.debug ? config.debug_xlsx.dest_lua : config.xlsx.dest_lua);
    let filePath = path.join(__dirname,config.debug ? config.debug_xlsx.src : config.xlsx.src);
    fs.readdir(filePath, function (err:any, files:any[]) {        
        if (err) {
            console.error("exportJson error:", err);
            throw err;
        }
        
        if (config.json.merge) {
            xlsx.mergeJson(files, filePath, pathDestJson, pathDestTs, pathDestLua)
        } else {
            files.forEach(item => {
                let pathItem = path.join(__dirname, item);
                xlsx.toJson(pathItem, pathDestJson, pathDestTs, pathDestLua);
            });
        }

    })
}
